<?php namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Industry;
use App\Profession;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Auth;

class SystemController extends Controller
{
    public function setProfession(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $id   = $request->get("id");
        $pid  = $request->get("pid");
        $name = $request->get("name");

        if(!is_null($pid) && $pid != 0){
            /** @var Profession $thatProfession */
            $thatProfession = Profession::whereId($pid)->first();
            if(!$thatProfession)
                return response()->json(['level'=>'error','message'=>'当前行业不存在']);

            $childProfession = new Profession();
            $childProfession->pro_name = $name;
            $childProfession->pid      = $pid;
            $childProfession->save();
        }else{

            /** @var Profession $thatProfession */
            $thatProfession = Profession::whereId($id)->first();
            if(!$thatProfession){
                $thatProfession = new Profession();
            }

            $thatProfession->pro_name = $name;
            $thatProfession->save();

        }


        return response()->json(['level'=>'success','message'=>'设置完成']);
    }

    public function delProfession(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $id = $request->get('id');

        /** @var Profession $thatProfession */
        $thatProfession = Profession::whereId($id)->first();
        if(!$thatProfession){
            return response()->json(['level'=>'error','message'=>'当前行业或职位不存在']);
        }

        $thatProfession->delete();


        return response()->json(['level'=>'success','message'=>'删除成功',"data"=>$thatProfession]);
    }

    public function setIndustry(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $id   = $request->get("id");
        $pid  = $request->get("pid");
        $name = $request->get("name");

        if(!is_null($pid) && $pid != 0){
            /** @var Industry $thatProfession */
            $thatProfession = Industry::whereId($pid)->first();
            if(!$thatProfession)
                return response()->json(['level'=>'error','message'=>'当前产业不存在']);

            $childProfession = new Industry();
            $childProfession->name = $name;
            $childProfession->pid  = $pid;
            $childProfession->save();
        }else{

            /** @var Industry $thatProfession */
            $thatProfession = Industry::whereId($id)->first();
            if(!$thatProfession){
                $thatProfession = new Industry();
            }

            $thatProfession->name = $name;
            $thatProfession->save();

        }


        return response()->json(['level'=>'success','message'=>'设置完成']);
    }

    public function delIndustry(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $id = $request->get('id');

        /** @var Industry $thatProfession */
        $thatProfession = Industry::whereId($id)->first();
        if(!$thatProfession){
            return response()->json(['level'=>'error','message'=>'当前产业不存在']);
        }

        $thatProfession->delete();


        return response()->json(['level'=>'success','message'=>'删除成功',"data"=>$thatProfession]);
    }
}